import os

from langchain import hub
from langchain.agents import AgentExecutor, create_react_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_openai import ChatOpenAI


# 创建新的AI agent
def createNewAgent():
    # temperature 在 0- 1之间 ，越大越随机，越小越固定
    llm = ChatOpenAI(
        temperature=0.2,
        model="",
        openai_api_key="",
        openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
    )
    os.environ["TAVILY_API_KEY"] = ""
    tools = [TavilySearchResults(max_results=1)]
    prompt = hub.pull("hwchase17/react")
    agent = create_react_agent(llm, tools, prompt)
    agent_executor = AgentExecutor(agent=agent, tools=tools,
                                   verbose=True, handle_parsing_errors=True,
                                   max_iterations=10)


# 获取AI 结果
def getAIResult(input):
    result = None
    if agent_executor:
        result = agent_executor.invoke({"input": input})
    return result
